package com.situ.blog.dao.impl;

import com.situ.blog.dao.ITypeDao;
import com.situ.blog.pojo.Type;
import com.situ.blog.pojo.query.TypeQuery;
import com.situ.blog.util.JDBCUtil;
import com.situ.blog.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TypeDaoImpl implements ITypeDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Type> selectAll() {
        String sql = "SELECT id,name,is_deleted,create_time,update_time FROM type";
        return template.query(sql, new BeanPropertyRowMapper<>(Type.class));
    }

    @Override
    public List<Type> selectByPage(TypeQuery typeQuery) {
        String sql = "SELECT id,name,image,click_count,content,type_id,status,is_deleted,create_time,update_time FROM type WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = typeQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        sql += " LIMIT ?,?";
        int offset = (typeQuery.getPage() - 1) * typeQuery.getLimit();
        queryList.add(offset);
        queryList.add(typeQuery.getLimit());

        List<Type> list = template.query(sql, new BeanPropertyRowMapper<>(Type.class), queryList.toArray());
        return list;
    }

    @Override
    public int selectTotalCount(TypeQuery typeQuery) {
        String sql = "SELECT count(*) FROM type WHERE 1=1";
        //构造一个集合存放所有的条件
        List<Object> queryList = new ArrayList<>();
        String queryName = typeQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }

        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void deleteById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "DELETE FROM type WHERE id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            int count = preparedStatement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, preparedStatement, null);
        }
    }

    @Override
    public void add(Type type) {
        String sql = "INSERT INTO type(name) VALUES (?)";
        template.update(sql, type.getName());
    }

    @Override
    public Type selectById(int id) {
        String sql = "SELECT id,name,is_deleted,create_time,update_time WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Type.class), id);
    }

    @Override
    public void update(Type type) {
        String sql = "UPDATE type SET name=? WHERE id=?";
        template.update(sql, type.getName(), type.getId());
    }
}
